# -*- coding = utf-8 -*-
# @Time : 2023/5/9 15:48
# @Author :Henry L
# @File : HLJ.py
# @Software : PyCharm
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
import os,glob


class JS:
    def __init__(self):
        options = webdriver.ChromeOptions()
        prefs = {"plugins.always_open_pdf_externally": True,
                 "download.default_directory": os.getcwd() + "\\"}
        options.add_experimental_option("prefs", prefs)
        self.url = 'http://ybj.jiangsu.gov.cn/col/col85454/index.html'
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        div1 = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div[1]/div/div[1]")
        div2 = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div[1]/div/div[2]")
        div3 = self.driver.find_element(By.XPATH, "/html/body/div/div[3]/div[1]/div/div[3]/div[1]")
        div4 = self.driver.find_element(By.XPATH, "/html/body/div/div[3]/div[1]/div/div[3]/div[2]")
        div5 = self.driver.find_element(By.XPATH, "/html/body/div/div[3]/div[1]/div/div[4]/div[1]")
        div6 = self.driver.find_element(By.XPATH, "/html/body/div/div[3]/div[1]/div/div[4]/div[2]")
        for div in [div1, div2, div3, div4, div5, div6]:
            lan = div.find_elements(By.TAG_NAME, "ul")
            if len(lan) == 1:
                for item in lan:
                    time.sleep(3)
                    li = item.find_elements(By.TAG_NAME, "li")
                    for a in li:
                        date = a.find_element(By.TAG_NAME, "span").text
                        a.find_element(By.TAG_NAME, "a").click()
                        handles = self.driver.window_handles
                        self.driver.switch_to.window(handles[-1])
                        time.sleep(1.5)
                        source = "江苏省公共资源交易中心"
                        title = self.driver.find_element(By.CLASS_NAME,
                                                         "title").text
                        view_time = self.driver.find_element(By.ID, "artcount")
                        content = self.driver.find_element(By.CLASS_NAME, "zoom")
                        attachments = content.find_elements(By.TAG_NAME, "a")
                        attachments_list = []
                        if len(attachments) > 0:
                            for attach in attachments:
                                time.sleep(1)
                                if attach.text == "" or attach is None or attach.text == "（一）总体要求":
                                    continue
                                if "附件" in attach.text or "filename" in attach.get_attribute("href"):
                                    self.driver.execute_script("window.scroll(100,1000)")
                                    attach.click()
                                    # a_name = attach.get_attribute("href").replace("/hljggzy/", '').replace(attach.text, '')
                                    # attachments_list.append(a_name)
                        self.driver.close()
                        handles = self.driver.window_handles
                        self.driver.switch_to.window(handles[-1])
                        data_list = [title, date, source, view_time, content, attachments_list]
                        print(data_list)
            else:
                list_li = lan[0].find_elements(By.TAG_NAME, "li")
                for num in range(0, len(list_li)):
                    button = list_li[num]
                    time.sleep(1)
                    ActionChains(self.driver).move_to_element(button).perform()
                    for a in lan[1 + num].find_elements(By.TAG_NAME, "li"):
                        date = a.find_element(By.TAG_NAME, "span").text
                        a.find_element(By.TAG_NAME, "a").click()
                        handles = self.driver.window_handles
                        self.driver.switch_to.window(handles[-1])
                        time.sleep(1.5)
                        source = "江苏省公共资源交易中心"
                        title = self.driver.find_element(By.CLASS_NAME,
                                                         "title").text
                        view_time = self.driver.find_element(By.ID, "artcount")
                        content = self.driver.find_element(By.CLASS_NAME, "zoom")
                        attachments = content.find_elements(By.TAG_NAME, "a")
                        attachments_list = []
                        if len(attachments) > 0:
                            for attach in attachments:
                                if attach.text == "" or attach.get_attribute("href") is None or attach.text == "（一）总体要求":
                                    continue
                                if "附件" in attach.text or "filename" in attach.get_attribute("href"):
                                    self.driver.execute_script("window.scroll(100,1000)")
                                    attach.click()
                                    # a_name = attach.get_attribute("href").replace("/hljggzy/", '').replace(attach.text, '')
                                    # attachments_list.append(a_name)
                        self.driver.close()
                        handles = self.driver.window_handles
                        self.driver.switch_to.window(handles[-1])
                        data_list = [title, date, source, view_time, content, attachments_list]
                        print(data_list)


    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

js = JS()
js.get("")
js.close()

